package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.user.mapper.AuthMapper;
import com.heima.user.service.AuthService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUserRealname;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class AuthServiceImpl extends ServiceImpl<AuthMapper, ApUserRealname> implements AuthService {

    @Autowired
    private AuthMapper authMapper;
    @Autowired
    private IWemediaClient wemediaClient;

    /**
     * 查询用户审核列表
     *
     * @param map
     * @return
     */
    @Override
    public ResponseResult getList(Map<String, Integer> map) {

        if (map.size() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        IPage<ApUserRealname> page = new Page<>(map.get("page"),map.get("size"));

        authMapper.selectPage(page, Wrappers.<ApUserRealname>lambdaQuery()
                .eq(map.get("status")!=null,ApUserRealname::getStatus,map.get("status")));



        PageResponseResult pageResponseResult = new PageResponseResult((int)page.getCurrent(),(int)page.getPages(),(int)page.getTotal());
        pageResponseResult.setData(page.getRecords());

        return pageResponseResult;
    }

    /**
     * 审核失败
     *
     * @param map
     * @return
     */
    @Override
    public ResponseResult authFail(Map<String, String> map) {
        if (map.size() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserRealname realname = authMapper.selectById(Integer.valueOf(map.get("id")));
        realname.setStatus((short)2);
        realname.setReason(map.get("msg").toString());
        authMapper.updateById(realname);
        return ResponseResult.okResult("审核失败");
    }

    /**
     * 审核通过
     *
     * @param map
     * @return
     */
    @Override
    public ResponseResult authPass(Map<String, String> map) {
        if (map.size() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserRealname realname = authMapper.selectById(map.get("id"));
        realname.setStatus((short)9);
        realname.setReason(map.get("msg").toString());
        authMapper.updateById(realname);


        //同步
        wemediaClient.authPass(realname);

        return ResponseResult.okResult("审核成功");
    }
}
